﻿#include "CGame.h"

CGame::CGame(HINSTANCE hIns,int w,int h,char *name) {
	this->hInstance = hIns;
	this->width = w;
	this->height = h;
	this->hWnd = NULL;
	strcpy(this->windowName,name);
}

CGame::~CGame(void) {

}
// hàm xử lý cửa sổ
LRESULT CALLBACK CGame::WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) {
	switch(msg) {
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd,msg,wParam,lParam);
}
// tạo cửa sổ CGame, cài đặt các đối tượng direct X ở đây
bool CGame::InitWindow() {
	WNDCLASSEX wc = {0};
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hbrBackground = (HBRUSH)(BLACK_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = (WNDPROC)WndProc;
	wc.lpszClassName = APP_TITLE;
	wc.lpszMenuName = NULL;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	if(!RegisterClassEx(&wc)) {
		return false;
	}
	this->hWnd = CreateWindow(APP_TITLE,windowName,
								WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,0,0,
								width,height,NULL,NULL,hInstance,NULL);
	if(!hWnd) {
		return false;
	}
	ShowWindow(hWnd,SW_NORMAL);
	UpdateWindow(hWnd);

	// tạo màn hình vẽ
	graphic = new CGraphic(this->hWnd, SCREEN_WIDTH, SCREEN_HEIGHT);
	if (!graphic->InitD3D()) {
		return false;
	}

	content = new CContent(graphic->GetDevice());

	return true;
}
// khởi tạo dữ liệu CGame
void CGame::InitData() {
	// nạp dữ liệu
	CAnimal	*animal = new CAnimal;
	animal->Init(this->content,D3DXVECTOR2(100,10),"Images\\ga.png");
	lstAnimal.push_back(animal);
	/*
	egg = new CEgg;
	egg->Init(this->content,D3DXVECTOR2(50,110),"Images\\Egg.png");
	*/
	keyboard = new CKeyboard(hInstance,hWnd);
	keyboard->Init();
	//mouse = new CMouse(hInstance,hWnd);
	//mouse->Init();
	ro = new CRo;
	ro->Init(this->content,D3DXVECTOR2(50,470),"Images\\Ro.png");
}
// cập nhật CGame
void CGame::Update(float gameTime) {
	for(list<CAnimal*>::iterator it = lstAnimal.begin(); it != lstAnimal.end(); it++) {
		(*it)->Update(gameTime);
		
		if((*it)->CheckTimeToLayingEgg()) {
			CEgg *egg = new CEgg;
			egg->Init(this->content,(*it)->position,"Images\\egg.png");
			egg->Update(gameTime);
			lstEgg.push_back(egg);
		}
		
	}
	//egg->Update(gameTime);
	// lấy trạng thái bàn phím
	keyboard->GetState();
	// kiểm tra nhấn phím
	if(keyboard->IsKeyDown(DIK_G)) {
		//keyboard->Reset();
		CAnimal	*animal = new CAnimal;
		animal->Init(this->content,D3DXVECTOR2(100,10),"Images\\ga.png");
		lstAnimal.push_back(animal);
	} else if(keyboard->IsKeyDown(DIK_N)) {
		//keyboard->Reset();
		CAnimal	*animal = new CAnimal;
		animal->Init(this->content,D3DXVECTOR2(100,10),"Images\\ngan.png");
		lstAnimal.push_back(animal);
	} else if(keyboard->IsKeyDown(DIK_V)) {
		//keyboard->Reset();
		CAnimal	*animal = new CAnimal;
		animal->Init(this->content,D3DXVECTOR2(100,10),"Images\\vit.png");
		lstAnimal.push_back(animal);
	}
	// kiểm tra phím mũi tên trái được nhấn
	if(keyboard->IsKeyDown(DIK_LEFT)) {
		ro->Update(gameTime,false);
	} else if(keyboard->IsKeyDown(DIK_RIGHT)) {
		ro->Update(gameTime,true);
	}
	// kiểm tra trạng thái chuột
	/*
	int x = mouse->CheckMouseMoveX();
	if((0 < x) && (x < SCREEN_WIDTH)) {
		ro->Update(gameTime,x);
	}
	*/
}
// vẽ các đối tượng trong CGame
void CGame::Render() {
	// bắt đầu vẽ
	graphic->Begin();
	for(list<CAnimal*>::iterator it = lstAnimal.begin(); it != lstAnimal.end(); it++) {
		(*it)->Render(graphic);
		
		if(lstEgg.size() > 0) {
			for(list<CEgg*>::iterator jt = lstEgg.begin(); jt != lstEgg.end(); jt++) {
				(*jt)->Render(graphic);
			}
		}
		
	}
	//egg->Render(graphic);
	ro->Render(graphic);
	graphic->End();
}

void CGame::Release() {
	if(lstAnimal.size() > 0) {
		for(list<CAnimal*>::iterator it = lstAnimal.begin(); it != lstAnimal.end(); it++) {
			(*it)->Shutdown();
			*it = NULL;
			delete (*it);
		}
		lstAnimal.clear();
	}
	
	if(lstEgg.size() > 0) {
		for(list<CEgg*>::iterator it = lstEgg.begin(); it != lstEgg.end(); it++) {
			(*it)->ShutDown();
			*it = NULL;
			delete (*it);
		}
		lstEgg.clear();
	}
	
	/*
	if(egg) {
		egg->ShutDown();
	}
	*/
	ro->Shutdown();
	ro = NULL;
	delete ro;
	graphic = NULL;
	delete graphic;
	content = NULL;
	delete content;
	keyboard->Shutdown();
	keyboard = NULL;
	delete keyboard;
	//mouse->Shutdown();
	//mouse = NULL;
	//delete mouse;
}
